<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\video\model;

/**
 * Description of live
 * 直播
 * @author zmh
 */
class GiftModel extends \web\common\model\BaseModel{
    
    protected function _initialize(){
        $this->tableName = 'live_gift';
    }

    
    // public function getDataList($pageIndex = -1, $pageSize = -1, $filter = '',$fileds='*', $order = 'id desc') {
    //     $userM = new \addons\member\model\MemberAccountModel();
    //     $sql = 'select a.*,u.real_name from '.$this->getTableName() . ' a left join '.$userM->getTableName().' u on u.id=a.user_id where 1=1 '.$filter;
    //     return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    // }

    /**
     * 发送礼物给主播
     */
    public function sendGift($user_id, $gift_id, $anchor_id,$click_count=1) {
        if($click_count<1) return $this->failData('数量错误');
        $user = (new \addons\member\model\MemberAccountModel())->getDetail($anchor_id,'id');
        if(!$user) return $this->failData('用户不存在');
        $MemberBalanceM = new \addons\member\model\Balance();
        $TradingRecordM = new \addons\member\model\TradingRecord();
        $coin_type = 3;

        $gift_info = $this->find($gift_id);
        if (!$gift_info)
            return $this->failData('当前礼物不存在');
        $amount = $gift_info['gift_amount']*$click_count;

        $kof_price = (new \web\common\model\sys\SysParameterModel)->getValByName('kof_price');
        $amount = round(($amount / $kof_price), 4);

        $myAmount = $MemberBalanceM->getBalanceByType($user_id, $coin_type);
        if ($amount > $myAmount['amount'])
            return $this->failData('余额不足');
        $MemberBalanceM->startTrans();
        //todo 添加数量字段,count 要有总额
        $res1 = (new \addons\video\model\GiftLog())->addLog($user_id, $gift_id, $anchor_id, $click_count);
        if (!$res1) {
            $MemberBalanceM->rollback();
            return $this->failData('添加礼物记录失败');
        }
        $res2 = $MemberBalanceM->updateBalance($user_id, $coin_type, $amount);
        if (!$res2) {
            $MemberBalanceM->rollback();
            return $this->failData('扣除余额失败');
        }
        $res3 = $TradingRecordM->addRecord($user_id, $amount, $res2['before_amount'], $res2['amount'], $coin_type, 12, 0, $anchor_id, '发送礼物'.$click_count.'次');
        if (!$res3) {
            $MemberBalanceM->rollback();
            return $this->failData('添加扣款记录失败');
        }

        //利率，平台收取的手续费
        $rate = $gift_info['profit']/100;
        if($gift_info['profit'] > 0){
            $amount = bcmul($amount, $rate, 4);
            $res4 = $MemberBalanceM->updateBalance($anchor_id, $coin_type, $amount, 1);
            if (!$res4) {
                $MemberBalanceM->rollback();
                return $this->failData('扣除余额失败');
            }
            $res5 = $TradingRecordM->addRecord($anchor_id, $amount, $res4['before_amount'], $res4['amount'], $coin_type, 12, 1, $user_id, '收到礼物'.$click_count.'次');
            if (!$res5) {
                $MemberBalanceM->rollback();
                return $this->failData('添加扣款记录失败');
            }
        }

        $MemberBalanceM->commit();
        return $this->successData($gift_info);
    }

    /**
     * 获取礼物列表
     */
    public function getGiftList(){
        return $this->select();
    }
    


}
